package wiki.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wiki.generic.bar.SideBar;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile dei link presenti nella side bar
 */
public class LinkServlet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		HttpSession ses=req.getSession(false);
		
		if(ses!=null){
			
			boolean isDoc = (Boolean) ses.getAttribute("rights");
			if(isDoc){
				String corso=(String) ses.getAttribute("corso");
				boolean modify=req.getParameterMap().containsKey("mod");
				PrintWriter out=resp.getWriter();
				out.write("<html>\n");
				out.write("<head>\n");
				if(!modify){
					out.write("<title>Aggiunta Link</title>\n");
				} else {
					out.write("<title>Rinomina Link</title>\n");
				}
				out.write("<link rel='stylesheet' type='text/css' href='./html/aggiunta/link.css'/>\n");
				out.write("<script type='text/Javascript' src='./html/aggiunta/link.js'></script>\n");
				out.write("</head>\n");
				out.write("<body>\n");
				if(!modify){
					out.write("<div id='title'>Aggiunta Link</div>\n");
				} else{
					out.write("<div id='title'>Rinomina Link</div>\n");
				}
				out.write("<div id='contents'>\n");
				if(!modify){
					out.write("<form action='./newlink' method='post' name='modulo' id='modulo'>\n");
				} else{
					if(req.getParameterMap().containsKey("cat") && req.getParameterMap().containsKey("name")){
					out.write("<form action='./newlink?mod=mod&cat="+req.getParameter("cat")+"&name="+req.getParameter("name")+"' method='post' name='modulo' id='modulo'>\n");
					} else {
						resp.sendRedirect("./view");
					}
				}
				out.write("<table>\n");
				out.write("<div id='show_message'> I seguenti campi sono obbligatori*:</div>\n");
				if(!modify){
				out.write("<h3>Scegli la Categoria:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				
				SideBar sbar=Setup.getSetup().getMediator().getbService().getSideBar(corso);
				List<String> cat=sbar.getCategories();
				for (int i = 0; i < cat.size(); i++) {
					String c=cat.get(i);
					out.write("<li><input type='radio' class='stile' value=\""+c+"\" name=\"cat\"/>"+c+"</li>\n");
				}
				
				out.write("</ul>\n");
				out.write("<h3>oppure creane una nuova:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				out.write("<li>Nuova Categoria: <input class='testo' type='text'  name='nuovo' value='' id='selected'/> </li>\n");
				out.write("<br />\n");
				out.write("<span class='attento'>Attento! Se scegli di inserire una nuova categoria una eventuale selezione di categorie gi&agrave; esistenti non sar&agrave; considerata!</span>");
				out.write("</ul>\n");
				out.write("<br />\n");
				}
				out.write("<h3>Scegli il Nome del Link:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				out.write("<li><input class='testo' type='text'  name='nome' value='");
				if(modify){
					out.write(req.getParameter("name"));
				}
				out.write("' /></li>\n");
				out.write("</ul>\n");
				out.write("<br />\n");
				if(!modify){
				out.write("<h3>Scegli il Link:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				HashMap<String, String> pages=Setup.getSetup().getMediator().getpService().getPagesMap(corso);
				Set<String> keys=pages.keySet();
				for (Iterator<String> iterator = keys.iterator(); iterator.hasNext();) {
					String key =iterator.next();
					out.write("<li><input type='radio'  class='stile' value=\""+key+"\" name=\"link\"/>"+key+"</li>\n");
				}
				
				out.write("</ul>\n");
				out.write("<h3>oppure inseriscilo manualmente:</h3>\n");
				out.write("<ul id='fontstyle-list'>\n");
				
				out.write("<li>Link esterno: http://<input class='testo' type='text'  name='ext' value=''  id='selected' /></li>\n");
				out.write("<br />\n");
				out.write("<span class='attento'>Attento! Se scegli di inserire un link esterno una eventuale selezione di link gi&agrave; esistenti non sar&agrave; considerata!</span>");
				out.write("</ul>\n");
				out.write("<br />\n");
				out.write("<br />\n");
				}
				out.write("</table>\n");					
				out.write("<input type='submit' value='Crea!' id='bottone' name='submit' onclick=\"if(controllaVuoti('modulo')!=true){return false;}\" />\n");
				out.write("</form>\n");
				out.write("</div>\n");
				out.write("<div id='footer'>Powered by J&omega;Team</div>\n");
				out.write("</body>\n");
				out.write("</html>\n");
				out.close();
			} else{
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
			
		} else {
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
		boolean isDoc=(Boolean) ses.getAttribute("rights");
			if(isDoc){
				boolean modify=req.getParameterMap().containsKey("mod");
				String corso=(String) ses.getAttribute("corso");
				String newCat=new String();
				if(!modify){
					newCat=req.getParameter("nuovo");
					if(newCat==""){
						newCat=req.getParameter("cat");
						if(newCat==null){
							resp.sendRedirect("./html/errorpages/nocategoria.html");
							return;
						}
					}
				}else {
					newCat=req.getParameter("cat");
				}
				//newCat=newCat.replace(" ", "_");
				newCat=Setup.getSetup().getMediator().getGpService().changeHTMLText(newCat);
				String name=req.getParameter("nome");
				if(name==null || name.equals("")){
					name="___";
				}
				//name=name.replace(" ", "_");
				name=Setup.getSetup().getMediator().getGpService().changeHTMLText(name);
				String oldName=new String();
				if(modify){
					oldName=req.getParameter("name");
				}
				String URL=new String();
				if(!modify){
					URL=req.getParameter("ext");
					if(URL==""){
						URL=req.getParameter("link");
						if(URL==null){
							resp.sendRedirect("./html/errorpages/nourl.html");
							return;
						}
						URL="./view?page="+URL;
					} else {
						URL="http://"+URL;
					}
				}
				
				if(!modify){
					Setup.getSetup().getMediator().getbService().addNewLink(corso,newCat, name, URL);
				} else {
					Setup.getSetup().getMediator().getbService().setNewLinkName(corso, newCat, oldName, name);
				}
				String current=(String) ses.getAttribute("current");
				resp.sendRedirect("./view?page="+current);
			} else{
				resp.sendRedirect("./html/errorpages/nopermessi.html");
				return;
			}
		} else{
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
				
	}
	
	
	
	
}
